<template>
    <page-wrapper>
        <table-search @search="search(1)" @reset="reset()">
            <template slot="form">
                <form-item label="">
                    <a-input v-model.trim="form.title" placeholder="问题标题" />
                </form-item>
                <form-item label="">
                    <a-select v-model="form.status" style="width:120px;" allowClear placeholder="请选择状态">
                        <a-select-option :value="0">未解决</a-select-option>
                        <a-select-option :value="2">已解决</a-select-option>
                    </a-select>
                </form-item>
            </template>
            <template slot="extra">
                <a-button
                    class="filter-item"
                    type="primary"
                    :disabled="hasPerm(['sys:question:add'])"
                    icon="plus"
                    @click="addInfo()"
                    >新增问题</a-button
                >
            </template>
        </table-search>
        <div class="table_scroll">
            <table-list
                :loading="loading"
                :columns="columns"
                rowKey="id"
                :pagination="pagination"
                :selection="selection"
                :data="list"
                :scroll="{ x: 1300 }"
            >
                <template slot="type" slot-scope="{ text }">
                    <field-tag :value="text" type="value" showType="name" :data="questionTypes"></field-tag>
                </template>

                <template slot="status" slot-scope="{ text }">
                    <span style="color:#52c41a;" v-if="text.status == 2">已处理</span>
                    <span style="color:#faad14" v-else>未处理</span>
                </template>
                <template slot="action" slot-scope="{ text }">
                    <a-button
                        size="small"
                        v-if="text.status != 2"
                        type="primary"
                        class="btn_space"
                        :disabled="hasPerm(['sys:question:update']) || text.status == 2"
                        @click="addInfo(text)"
                        >处理问题</a-button
                    >
                    <a-button
                        size="small"
                        v-if="text.status == 2"
                        :disabled="hasPerm(['sys:question:query'])"
                        @click="addInfo(text)"
                        >查看问题</a-button
                    >
                </template>
            </table-list>
        </div>

        <template v-if="isShowInfo">
            <question-info :info="itemInfo" @cancel="isShowInfo = false" @submit="submit" />
        </template>
    </page-wrapper>
</template>
<script>
import table from '@/mixins/table';
import { columns } from './columns';
import QuestionInfo from './components/info';
import { questionTypes } from './dictionary';
import { remoteList, updateInfo } from '@/api/question';

export default {
    name: 'UserList',
    mixins: [table],
    components: {
        QuestionInfo
    },
    data() {
        let originForm = {
            title: null,
            status: undefined
        };
        return {
            originForm,
            questionTypes,
            form: {
                ...originForm
            },
            columns,
            itemInfo: null,
            isShowInfo: false
        };
    },
    created() {
        this.search(1);
    },
    methods: {
        async search(page) {
            try {
                this.loading = true;
                if (page) {
                    this.form.page = page;
                }
                this.list = [];
                let res = await remoteList(this.form);
                let { list, total } = res.data;
                this.list = list;
                this.total = total;
                this.selectionBox = [];
            } finally {
                this.loading = false;
            }
        },
        //删除
        remove(item = '') {
            this.$confirm({
                title: '提示',
                content: '确定要删除公司？',
                okText: '确定',
                cancelText: '取消',
                onOk: async () => {
                    try {
                        let ids = [];
                        if (item) {
                            ids = [item.tenantId];
                        } else {
                            let selectionBox = this.selectionBox;
                            selectionBox.map(async item => {
                                ids.push(item.tenantId);
                            });
                        }
                        await deleteInfo({ ids });
                        this.$message.success('删除成功!');
                        this.search(1);
                    } catch (err) {}
                },
                onCancel() {}
            });
        },
        //重置密码
        resetPWD(item) {
            let that = this;
            this.$confirm({
                title: '提示',
                content: `确定要将 ${item.companyName} 的账号 ${item.tenantName} 的密码重置为:123456吗？`,
                okText: '确定',
                cancelText: '取消',
                onOk: async () => {
                    try {
                        let data = {
                            tenantCode: item.companyCode,
                            userName: item.tenantName,
                            userType: 'tenant_admin'
                        };
                        let res = await resetPassword(data);
                        that.$message.success(`已将 ${item.companyName} 的账号 ${item.tenantName} 的密码重置为:123456`);
                    } catch (err) {}
                },
                onCancel() {}
            });
        },
        //复制公司信息
        copyInfo(e, item) {
            let { companyName, companyCode, tenantName } = item;
            let copyText = `${companyName}：http://172.26.153.15:8888/${companyCode}/login；公司账号：${tenantName}；初始密码：123456`;
            handleClipboard(copyText, e, '账号');
        },
        //是否开启
        async onSwitchStatus(item) {
            try {
                let { status } = item;
                item.status = status == 1 ? 0 : 1;
                this.loading = true;
                await updateInfo(item);
                this.$message.success('操作成功!');
            } catch (err) {
                let { status } = item;
                item.status = status ? 0 : 1;
            } finally {
                this.loading = false;
            }
        },
        //显示添加用户
        addInfo(item = {}) {
            this.isShowInfo = true;
            this.itemInfo = item;
        },
        submit() {
            this.isShowInfo = false;
            this.itemInfo = {};
            this.search(1);
        }
    }
};
</script>
